package com.jdls.day19.work;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author 嘉德罗斯
 * @date 2021/8/10
 */
public class Test3 {
    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        //  需求：使用反射技术完成以下需求：
        //        1. 创建Person对象
        //        2. 给name属性赋值
        //        3. 调用eat方法和getName方法
        Person p = new Person();
        Class pc = Person.class;
        Field df = pc.getDeclaredField("name");
        df.setAccessible(true);
        df.set(p,"嘉德罗斯");
        System.out.println(p);
        Method dm = pc.getDeclaredMethod("eat", String.class);
        dm.invoke(p,"炸鸡");
        Method dm1 = pc.getDeclaredMethod("getName");
        dm1.setAccessible(true);
        System.out.println(dm1.invoke(p));
    }
}

